package top.huntwolf.admin.service;

import com.alibaba.excel.EasyExcel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import top.huntwolf.admin.common.enums.CodeMsg;
import top.huntwolf.admin.dao.BookDao;
import top.huntwolf.admin.dao.SubjectDao;
import top.huntwolf.admin.exception.ServiceException;
import top.huntwolf.admin.listener.SubjectExcelListener;
import top.huntwolf.admin.pojo.Subject;
import top.huntwolf.admin.pojo.excel.SubjectExcelData;
import top.huntwolf.admin.pojo.subject.FirstSubject;
import top.huntwolf.admin.pojo.subject.FirstSubjectWithBook;
import top.huntwolf.admin.pojo.subject.SecondSubject;
import top.huntwolf.admin.pojo.subject.SecondSubjectWithBook;
import top.huntwolf.admin.utils.UUIDUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class SubjectService {

    @Autowired
    private SubjectDao subjectDao;
    @Autowired
    private BookDao bookDao;

    // 读excel添加分类记录
    public void addSubjectByExcel(MultipartFile file, SubjectService subjectService) {
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectExcelData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public void save(Subject subject) {

        subjectDao.save(subject);
    }


    // 查询分类是否存在
    public Subject existSubject(String title, Integer pid) {

        Subject subject = new Subject();
        subject.setTitle(title).setParentId(pid);
        return subjectDao.FindSubject(subject);
    }

    // 获取所有的分类
    public List<FirstSubject> getList() {
        // 1、 查询所有的一级分类
        List<Subject> firstSubjects = subjectDao.FindFirstList();
        // 2、 查询所有的二级分类
        List<Subject> secondSubjectList = subjectDao.FindSecondList();

        // 定义最终返回的集合
        List<FirstSubject> finalSubject = new ArrayList<>();

        // 3、 封装一级分类
        for (Subject oneSubject : firstSubjects) {
            //将Subject封装到First中去
            FirstSubject firstSubject = new FirstSubject();
            BeanUtils.copyProperties(oneSubject, firstSubject); // 源 to object

            //定义一个二级分类的集合
            List<SecondSubject> secondSubjects = new ArrayList<>();
            // 4、 封装二级分类
            for (Subject twoSubject : secondSubjectList) {
                if (twoSubject.getParentId().equals(oneSubject.getId())) {
                    SecondSubject secondSubject = new SecondSubject();
                    BeanUtils.copyProperties(twoSubject, secondSubject);
                    secondSubjects.add(secondSubject);
                }
            }
            // 最后将这个二级分类的集合装入一级分类的children中
            firstSubject.setChildren(secondSubjects);
            // 将一级分类装入最终要返回的一级subject集合中去
            finalSubject.add(firstSubject);
        }

        return finalSubject;
    }

    // 根据名称获取分类subject
    public Subject findSubjectByTitle(String title) {
        return subjectDao.findSubjectByTitle(title);
    }

    public void update(Subject subject) {
        subjectDao.updateSubject(subject);
    }

    public List<FirstSubjectWithBook> getListInfo(){
        List<FirstSubject> subject = getList();
        ArrayList<FirstSubjectWithBook> res = new ArrayList<>();

        for (FirstSubject f : subject) {
            FirstSubjectWithBook firstSubjectWithBook = new FirstSubjectWithBook();
            ArrayList<SecondSubjectWithBook> bookList = new ArrayList<>();
            firstSubjectWithBook.setId(f.getId()).setTitle(f.getTitle()).setSecondSubjectWithBooks(bookList);

            f.getChildren().forEach(c -> {
                SecondSubjectWithBook secondSubjectWithBook = new SecondSubjectWithBook();
                secondSubjectWithBook.setId(c.getId()).setTitle(c.getTitle()).setSort(c.getSort());
                secondSubjectWithBook.setBookList(bookDao.selectBySubjectId(c.getId()));
                bookList.add(secondSubjectWithBook);
            });

            res.add(firstSubjectWithBook);
        }
        return res;
    }

    //根据subjectId删除分类
    public void deleteSubject(int subjectId) {
        subjectDao.deleteSubject(subjectId);
    }

    //------------------------- 以下为热门模块的功能 -----------------------------------------

    // 获取前六个热门分类
    public List getHotSort(Integer limit) {
        return subjectDao.getHotSort(limit);
    }

    // 按isbn查图书或分类查图书时，热门分类标签+1
    public void addSort(Integer subjectId) {

        // 根据分类id获取分类
        Subject subject = subjectDao.findSubjectBySubjectId(subjectId);
        subject.setSort(1);
        //将排序字段+1后修改排序字段
        subjectDao.updateSubjectSort(subject);

    }

    // 后台系统删除热门热搜
    public void removeHotSort(Integer subjectId) {
        if (subjectId == null) {
            throw new ServiceException(CodeMsg.PARAMETER_NULL);
        }
        subjectDao.removeHotSort(subjectId);
    }

    // 在数据量还没那么多时，由后台系统指定
    public void addHotSort(List<Subject> list) {
        subjectDao.addHotSort(list);
    }

    public void updateHotSort(List<Subject> list) {
        subjectDao.updateSubjectSorts(list);
    }
}
